package com.ruoyi.reservationserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.config.JwtConfig;
import com.ruoyi.reservationserver.dao.UserDao;
import com.ruoyi.reservationserver.pojo.RechargePojo;
import com.ruoyi.system.domain.CourtStatistic;
import com.ruoyi.reservationserver.entity.Members;
import com.ruoyi.system.domain.StadiumStatistic;
import com.ruoyi.reservationserver.entity.User;
import com.ruoyi.reservationserver.pojo.UserLoginPojo;
import com.ruoyi.system.service.CourtStatisticService;
import com.ruoyi.reservationserver.service.MembersService;
import com.ruoyi.reservationserver.service.StadiumStatisticService;
import com.ruoyi.reservationserver.service.UserService;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author makejava
 * @since 2023-07-04 16:21:49
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    private UserDao userDao;

    @Resource
    private MembersService membersService;

    @Resource
    private CourtStatisticService courtStatisticService;

    @Resource
    private StadiumStatisticService stadiumStatisticService;
    @Resource
    private JwtConfig jwtConfig;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    String encode;




    @PostMapping(value = "/login")
    //@RequestBody 表示接收是的Json格式的参数
    public Object login(@RequestBody UserLoginPojo loginPojo) {
        System.out.println("login:" + loginPojo);
        User user = userService.login(loginPojo.getAccount());
        if (user == null) {
            return Result.error("登录失败");
        }else {
            if (bCryptPasswordEncoder.matches(loginPojo.getPassword(),encode)){
                String token = jwtConfig.createToken(loginPojo.getAccount());
                return Result.success(user);
            }else {
                return Result.error("密码错误");
            }

        }
    }

    /**
     * 通过用户名查询用户
     *
     * @param account
     * @return
     */
    @GetMapping("selectUserByAccount")
    public Result selectUserByAccount(@RequestParam String account) {
        List<User> userList;
        if (account == null) {
            return Result.error("不能输入空信息");
        }
        userList = userDao.selectUserByAccount(account);
        return Result.success(userList);
    }

    /**
     * 新增球友
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody User user) {
        String account = user.getAccount();
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("account",account);

        if (userService.getOne(userQueryWrapper)!=null) {
            return Result.error("数据库存在该用户，请更换账号名");
        } else {
            user.setIsMember(0);
            encode = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(encode);
            this.userService.insert(user);
            Members members = membersService.insertMember(user.getUserId());
            if (members == null) {
                return Result.error("关联表出错");

            }
            QueryWrapper<CourtStatistic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stadium_id", 1);

            CourtStatistic courtStatistic = courtStatisticService.getOne(queryWrapper);
            courtStatistic.setUserNum(userService.count());
            courtStatisticService.update(courtStatistic);
            QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
            wrapper.eq("stadium_statistic_id", 1);
            StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
            stadiumStatistic.setTotalUserNum(userService.count());
            stadiumStatisticService.update(stadiumStatistic);
            return Result.success();
        }
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody User user) {
        try {
            System.out.println(user);
            if (user == null) {
                return Result.error("数据不能为空");
            }
            System.out.println(user.getUserId());
            if (user.getUserId() == null) {
                return Result.error("id不能为空");
            }
            this.userService.update(user);
            return Result.success();

        } catch (Exception e) {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Result deleteById(@RequestParam("userId") Integer id) {

        try {
            QueryWrapper<CourtStatistic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stadium_id", 1);

            CourtStatistic courtStatistic = courtStatisticService.getOne(queryWrapper);
            if (this.userService.deleteById(id)) {
                courtStatistic.setUserNum(userService.count());
                courtStatisticService.update(courtStatistic);

                QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
                wrapper.eq("stadium_statistic_id", 1);
                StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
                stadiumStatistic.setTotalUserNum(userService.count());
                stadiumStatisticService.update(stadiumStatistic);

            } else {
                return Result.error("500", "出错啦");
            }


            return Result.success();
        } catch (Exception e) {
            System.out.println(e);
            return Result.error("删除失败");
        }

    }
}
